<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Booth_products_model extends CI_Model {

    private $_table = 'booth_products';
    private $_primary = 'id';

    function create($data) {
        $this->db->insert($this->_table, $data);
        return $this->db->insert_id();
    }

    function update($id, $data) {
        $this->db->where($this->_primary, $id);
        return $this->db->update($this->_table, $data);
    }

    public function delete($id) {
        $this->db->where($this->_primary, $id);
        return $this->db->delete($this->_table);
    }

    public function getListBoothProducts($params = array()) {
        $condition = 'WHERE 1=1';
        $limit = '';

        if (isset($params['start']) && isset($params['limit'])) {
            $limit = "LIMIT {$params['start']}, {$params['limit']}";
        }

        if (isset($params['parent']) && $params['parent'] === TRUE) {
            $condition .= ' AND `parent_id` IS NULL';
        }
        if (isset($params['where'])) {
            $condition .= $params['where'];
        }

        $query = $this->db->query("SELECT *
  	                                   FROM `{$this->_table}`
  	                                   {$condition}
  	                                   ORDER BY `{$this->_primary}` DESC
  	                                   {$limit}");

        return $query->result_array();
    }

    function getBoothProductById($id) {
        $query = $this->db->query("SELECT *
  	                                   FROM  `{$this->_table}`
  	                                   WHERE `id` = {$id}");
        return $query->row_array();
    }

    function getListSpecialProducts($booth_id) {
        $query = $this->db->query("SELECT * 
  	                                   FROM `{$this->_table}`
  	                                   WHERE `booth_id` = {$booth_id}
  	                                   AND `product_type` IN(1,2)
  	                                   ORDER BY `{$this->_primary}` DESC");
        return $query->result_array();
    }

    // Cập nhật giá trị rate cho sản phẩm
    function changeRate($booth_product_id, $data) {
        $this->db->where($this->_primary, $booth_product_id);
        return $this->db->update($this->_table, $data);
    }
    

    function get_products_by_category_id($category_id) {
        $query = $this->db->query("SELECT `{$this->_table}`.*, booths.user_id, booths.slug as b_slug
  	                                   FROM `{$this->_table}`
  	                                   INNER JOIN `booths` ON `{$this->_table}`.`booth_id` = `booths`.`id`
  	                                   WHERE `{$this->_table}`.`category_id` = {$category_id}
  	                                   ORDER BY `{$this->_primary}` DESC");
        return $query->result_array();
    }

    public function getRandomProducts($limit) {
        $query = $this->db->query("SELECT `{$this->_table}`.*, `booths`.`user_id` AS `user_id`, `booths`.`slug` AS `booth_slug` 
  	                                   FROM `{$this->_table}`, `booths`
                                           WHERE `booths`.`id` = `{$this->_table}`.`booth_id`
  	                                   ORDER BY RAND()
                                           LIMIT 6");
        return $query->result_array();
    }
    
     function getOneBoothProductByBoothId($id) {
        $query = $this->db->query("SELECT *
  	                                   FROM  `{$this->_table}`
  	                                   WHERE `booth_id` = {$id} 
                                           ORDER BY RAND()
                                           LIMIT 1");
        return $query->row_array();
    }
}